<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

	<title>Crunchy :: Editor</title>
	<meta title="template local_template.html">
</head>

<body>
<div id="main_title">Editor</div>
<div id="content">

<div class="notes">
<h4>Minor annoyance / Bug :-(</h4>
<p> Sometimes, seemingly at random,
you may have to click on the Execute button a second time to get it
working properly.
</p>
</div>

<p>Sometimes, you want to write Python code that is <i>*gasp*</i> worth saving.
In this case, rather than using the Python interpreter,
you probably resort to your favourite editor which most likely allows you to run your Python code.
Normally, you would need to start your favourite editor,
and type in (or cut-and-paste) the following example,
most of which has been shamelessly lifted from the official Python tutorial.</p>
<pre title="editor size=(8, 80)">
for n in range(2, 10):
    for m in range(2, n):
        if n % m == 0:
            print('%d equals %d * %d' % ( n, m, n/m))
            break
    else: # loop fell through without finding a factor
        print('%d is a prime number' % n)

# The following will raise an error as "a" is not defined.
print(a)
</pre>
<div class="notes">
<h4>Save and run</h4>
<p> Using the editor, you can click on the save program icon and will be given the possibility
to save a program in a specific location and have Python execute it from there.
</p>
</div>
<p>However, if you read this page using Crunchy and your favourite browser, you will see that a "text box" with a handy button is located just in between this paragraph and the piece of code above.  So, by using Crunchy, you don't need to use an editor to actually try the above <i> long</i> example. Furthermore, you will notice a check box labeled "Toggle editor".
If you click on it, you will see appear on the page a real editor, provided by Christophe Dolivet.  If you are interested in this editor, search for EditArea on the Internet.  Once the editor is toggled, its size can be changed to fit the code better.  When toggled back to a simple textarea, it will retain its new size.</p>
<p>If you do try the example, you will notice that "errors" (output from stderr) are printed in a different colour than "normal" stuff (output from stdout).</p>


<h3 >Advanced stuff, for tutorial writers</h3>
<p>When code in a code editor is executed, a new thread is started by Crunchy unless
the "save and run" feature is used, in which case a separate process is started.  Neither the separate process nor the new thread have access to variables defined or modules imported in other environments. The required vlam code is as follows.</p>
<pre title="html">
&lt;pre title="editor [no_copy] [no_pre] [linenumber [=start_number]]
                   [external [no_internal]]"&gt;
Some Python code

&lt;/pre&gt;</pre>
This adds a code editor (enhanced html <code title="html">&lt;textarea&gt;</code>)
to the current page.
<p>By default, Crunchy copies the contents of the <code title="html">&lt;pre&gt;</code>
in the original file into a new <code title="html">&lt;pre&gt;</code> above the editor;
however, this is prevented if
<code title="python">"no_pre"</code> is present.
Also by default, Crunchy copies the contents of the original
<code title="html">&lt;pre&gt;</code>
into the editor; however, this is prevented if
<code title="python">"no_copy"</code> is present.
If the <code title="python">"linenumber"</code> option is present,
a line number will appear before each line of input code.</p>
<p>The <code title="python">"[external [no_internal]]"</code>
options will be explained later.</p>


</div>

</body>
</html>
